﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib;
using GestionePolveri2.Filters;
using GestionePolveriLib.BL.FilterBags;
using BrightIdeasSoftware;
using GestionePolveriLib.Facades.Reports;
using FrameworkLib.Utilities.Export;
using FrameworkLib.Configurations.FormsConfiguration;
using GestionePolveriLib.BL;
using GestionePolveriLib.BL.Reports;
using FrameworkLib.Utilities.Sorting;
using FrameworkLib.Configurations.FormsConfiguration.BL;

namespace GestionePolveri2
{
    public partial class ReportDirezionaleForm : ReportDirezionaleBase
    {
        string[] columnsToHide = null;
        OLVColumn sortColumn = null;
        SortOrder m_sortOrder = SortOrder.Ascending;
        SplashPage sp = null;
        GestionePolveriLib.BL.Reports.ReportDirezionale rdTotali = null;
        TotalViewerDialog m_tvd = null;

        public ReportDirezionaleForm()
        {
            SetupColumns();

            InitializeComponent();
        }

        public override void InitializeForm()
        {
            System.Security.Principal.IPrincipal m_operatore = System.Threading.Thread.CurrentPrincipal;
            string[] roles = System.Web.Security.Roles.GetRolesForUser(m_operatore.Identity.Name);

            SavingConfigurationBag scb = new SavingConfigurationBag();
            scb.FormName = this.Name;
            scb.RoleName = roles[0];
            this.SavingConfigurationBag = scb;

            base.InitializeForm();

            this.ShowCNFGMenu = true;

            this.AddCustomButton("btnEsportaToExcel", "Esporta in Excel");
            //this.AddCustomButton("btnShowTotals", "Mostra Totali");

            this.CanModify = false;
            this.ShowOnlyCustomButton = true;

            this.ItemSelected += new EventHandler<CustomEventArgs<GestionePolveriLib.BL.Reports.ReportDirezionale>>(Anagrafica_ItemSelected);

            this.objectListView1.OwnerDraw = true;
            this.objectListView1.OwnerDrawnHeader = true;
            //this.objectListView1.ItemChecked += new ItemCheckedEventHandler(objectListView1_ItemChecked);
            //this.objectListView1.FormatCell += new EventHandler<FormatCellEventArgs>(objectListView1_FormatCell);
            //this.objectListView1.CellClick += new EventHandler<CellClickEventArgs>(objectListView1_CellClick); 

            this.objectListView1.UseCellFormatEvents = true;
            
            NascondiColonne();
        }

        protected override void OnLoadCompleted()
        {
            base.OnLoadCompleted();

            this.objectListView1.GridLines = true;
            this.objectListView1.ShowGroups = false;

            this.IsFiltering = true;
            this.Filter.FilterBag.Ordinamento = 2;

            this.ManageData();
        }

        //public override void OnSalvaConfigurazioneMaschera()
        //{
        //    base.OnSalvaConfigurazioneMaschera();

        //    byte[] data = this.objectListView1.SaveState();

        //    ColumnsConfigurationManager ccm = new ColumnsConfigurationManager();
        //    ccm.FormName = this.Name;
        //    ccm.State = data;
        //    ccm.SaveState();
        //}

        //public override void OnRipristinaConfigurazioneDefaultMascheraClicked()
        //{
        //    base.OnRipristinaConfigurazioneDefaultMascheraClicked();
        //}

        private void SetupColumns()
        {
            this.objectListView1.SuspendLayout();

            CreateColumn("Applicazione", "Tipo", 69, "");
            CreateColumn("CodicePolvere", "Codice Polvere", 69, "");
            CreateColumn("Note", "Note Polvere", 69, "");
            CreateColumn("Famiglia", "Famiglia", 69, "");
            CreateColumn("Descrizione", "Descrizione", 69, "");
            CreateColumn("Fornitore", "Fornitore", 69, "");
            CreateColumn("ColoriAbbinati", "Colori Abbinati", 69, "");
            CreateColumn("ScortaMinima", "Scorta Minima", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("PrezzoUnitario", "Ultimo Prezzo", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Giacenza", "Giacenza [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("GiacenzaXValore", "Valore €", 69, "{0:C}", HorizontalAlignment.Right);
            CreateColumn("Carico", "Carico [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Scarico", "Scarico [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NumeroPrelievi", "Numero Prelievi", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_RAL1", "RAL1 [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_RAL1", "RAL1 Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_RAL2", "RAL2 [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_RAL2", "RAL2 Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_Verticale", "Verticale [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_Verticale", "Verticale Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_Sublimato", "Sublimato [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_Sublimato", "Sublimato Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_Ferro", "Ferro [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_Ferro", "Ferro Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_ColorwoodGrande", "Colorwood Grande [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_ColorwoodGrande", "Colorwood Grande Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_ColorwoodPiccolo", "Colorwood Piccolo [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_ColorwoodPiccolo", "Colorwood Piccolo Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_Reso", "Reso [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_Reso", "Reso Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_Smaltito", "Smaltito [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_Smaltito", "Smaltito Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_Venduto", "Venduto [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_Venduto", "Venduto Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_Correzione", "Inventario [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_Correzione", "Inventario Np.", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("Cons_AllineamentoInventario", "Allineamento Inventario [Kg]", 69, "{0:N}", HorizontalAlignment.Right);
            CreateColumn("NP_AllineamentoInventatio", "Allineamento Inventario Np.", 69, "{0:N}", HorizontalAlignment.Right);

            this.objectListView1.ResumeLayout();
        }

        private void CreateColumn(string propertyName, string text, int width, string format)
        {
            BrightIdeasSoftware.OLVColumn olvColumn = ((BrightIdeasSoftware.OLVColumn)(new BrightIdeasSoftware.OLVColumn()));
            
            objectListView1.AllColumns.Add(olvColumn);

            objectListView1.Columns.Add((System.Windows.Forms.ColumnHeader)olvColumn);

            olvColumn.AspectName = propertyName;
            olvColumn.AspectToStringFormat = (!string.IsNullOrWhiteSpace(format) ? format : string.Empty);
            olvColumn.Text = text;
            olvColumn.Width = width;
            olvColumn.TextAlign = HorizontalAlignment.Left;
        }

        private void CreateHiddenColumn(string propertyName)
        {
            BrightIdeasSoftware.OLVColumn olvColumn = ((BrightIdeasSoftware.OLVColumn)(new BrightIdeasSoftware.OLVColumn()));

            objectListView1.AllColumns.Add(olvColumn);

            objectListView1.Columns.Add((System.Windows.Forms.ColumnHeader)olvColumn);

            olvColumn.AspectName = propertyName;
            olvColumn.Text = propertyName;
            olvColumn.IsVisible = true;
            olvColumn.Width = 0;
        }

        private void CreateColumn(string propertyName, string text, int width, string format, HorizontalAlignment alignment)
        {
            BrightIdeasSoftware.OLVColumn olvColumn = ((BrightIdeasSoftware.OLVColumn)(new BrightIdeasSoftware.OLVColumn()));

            objectListView1.AllColumns.Add(olvColumn);

            objectListView1.Columns.Add((System.Windows.Forms.ColumnHeader)olvColumn);

            olvColumn.AspectName = propertyName;
            olvColumn.AspectToStringFormat = (!string.IsNullOrWhiteSpace(format) ? format : string.Empty);
            olvColumn.Text = text;
            olvColumn.Width = width;
            olvColumn.TextAlign = alignment;
        }

        protected override void OnButtonsBarClicked(CustomEventArgs<GestionePolveriLib.BL.Reports.ReportDirezionale> e)
        {
            base.OnButtonsBarClicked(e);
        }

        protected override void OnCustomButtonsBarClicked(CustomEventArgs<GestionePolveriLib.BL.Reports.ReportDirezionale> e)
        {
            base.OnCustomButtonsBarClicked(e);

            //if (e.ButtonName.Equals("btnShowTotals", StringComparison.InvariantCultureIgnoreCase))
            //{
            //    m_tvd = new TotalViewerDialog();

            //    CalcolaTotali((List<GestionePolveriLib.BL.Reports.ReportDirezionale>)this.objectListView1.Objects);

            //    m_tvd.TotalsContainer = rdTotali;

            //    // qui si nascondono le colonne da non visualizzare in base al ruolo...
            //    if (columnsToHide != null)
            //    {
            //        foreach (string s in columnsToHide)
            //        {
            //            m_tvd.ShowTotalElement(s, false);
            //        }
            //    }

            //    // qui si nascondono le colonne da non visualizzare perchè nascoste da configurazione...
            //    this.objectListView1.AllColumns.ForEach(x => m_tvd.ShowTotalElement(x.AspectName, x.IsVisible));
                
            //    //foreach (OLVColumn c in this.objectListView1.AllColumns)
            //    //{
            //    //    m_tvd.ShowTotalElement(c.AspectName, c.IsVisible);
            //    //}

            //    m_tvd.Show(this);
            //}

            if (e.ButtonName.Equals("btnEsportaToExcel", StringComparison.InvariantCultureIgnoreCase))
            {
                List<ReportDirezionale> result = null;

                if (sortColumn != null)
                {
                    bool sortOrder = (m_sortOrder == SortOrder.Ascending ? true : false);
                    IOrderedEnumerable<ReportDirezionale> result2 = ((List<ReportDirezionale>)this.objectListView1.Objects).OrderBy(sortColumn.AspectName, sortOrder);

                    result = result2.ToList();
                }
                else
                {
                    result = (List<ReportDirezionale>)this.objectListView1.Objects;
                }

                ReportDirezionaleFacade rdf = new ReportDirezionaleFacade(this.Filter.FilterBag);
                //rdf.RunAsync(() => rdf.GetReportData(), (result) =>
                //{
                    if (result != null)
                    {
                        OperatorePrincipal m_OperatorePrincipale = (OperatorePrincipal)System.Threading.Thread.CurrentPrincipal;
                        if ((!m_OperatorePrincipale.IsInRole("Amministrazione")) && (!m_OperatorePrincipale.IsInRole("Direzione")))
                        {
                            columnsToHide = new string[] { 
                                        "Cons_RAL1", "NP_RAL1", 
                                        "Cons_RAL2", "NP_RAL2", 
                                        "Cons_Verticale", "NP_Verticale", 
                                        "Cons_Ferro", "NP_Ferro", 
                                        "Cons_ColorwoodGrande", "NP_ColorwoodGrande", 
                                        "Cons_ColorwoodPiccolo", "NP_ColorwoodPiccolo",
                                        "Cons_Reso", "NP_Reso", 
                                        "Cons_Smaltito", "NP_Smaltito", 
                                        "Cons_Venduto", "NP_Venduto",
                                        "Cons_Correzione", "NP_Correzione",
                                        "Cons_AllineamentoInventario", "NP_AllineamentoInventatio"
                            };
                        }

                        //string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                        //string filename = string.Format(@"{0}\Prova.xls", path);

                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
                        sfd.FilterIndex = 1;
                        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            string errorMessage = string.Empty;
                            if (result.ToExcel(sfd.FileName, out errorMessage, columnsToHide))
                            {
                                MessageBox.Show("Esportazione in Excel effettuata!", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show(string.Format("ATTENZIONE: Si è verificato un errore: {0}", errorMessage), "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Nessun dato da inviare in stampa.", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                //});
            }
        }

        void Anagrafica_ItemSelected(object sender, CustomEventArgs<GestionePolveriLib.BL.Reports.ReportDirezionale> e)
        {
            base.Current = e.Item;
        }

        void Anagrafica_CustomButtonClicked(object sender, CustomEventArgs<GestionePolveriLib.BL.Reports.ReportDirezionale> e)
        {

        }

        public override void OnFilterExecuted()
        {
            base.OnFilterExecuted();

            this.objectListView1.Sorting = SortOrder.None;
            this.objectListView1.Unsort();

            this.IsFiltering = true;

            ManageData();
        }

        public override void OnFilterReset()
        {
            base.OnFilterReset();

            this.objectListView1.Sorting = SortOrder.None;
            this.objectListView1.Unsort();

            this.IsFiltering = true;
            this.Filter.FilterBag.Ordinamento = 2;

            ManageData();
        }

        public override void OnRefreshFilterData()
        {
            base.OnRefreshFilterData();

            this.objectListView1.Unsort();

            ManageData();
        }

        protected override void LoadData()
        {
            //sp = new SplashPage();
            //sp.ShowDialog();

            base.LoadData();

            ReportDirezionaleFacade rdf = new ReportDirezionaleFacade();
            rdf.RunAsync(() => rdf.GetReportData(), (result) =>
            {
                if (result != null)
                {
                    this.objectListView1.SetObjects((List<GestionePolveriLib.BL.Reports.ReportDirezionale>)result, true);
                    this.objectListView1.Refresh();
                }
            });
        }

        protected override void LoadData(ReportDirezionaleFilterBag filter)
        {
            //sp = new SplashPage();
            //sp.ShowDialog();

            base.LoadData(filter);

            ReportDirezionaleFacade rdf = new ReportDirezionaleFacade(filter);
            rdf.RunAsync(() => rdf.GetReportData(), (result) =>
            {
                if (result != null)
                {
                    result = CalcolaTotali((List<GestionePolveriLib.BL.Reports.ReportDirezionale>)result);

                    this.objectListView1.SetObjects((List<GestionePolveriLib.BL.Reports.ReportDirezionale>)result, true);
                    this.objectListView1.Refresh();
                }
            });
        }

        private List<GestionePolveriLib.BL.Reports.ReportDirezionale> CalcolaTotali(List<GestionePolveriLib.BL.Reports.ReportDirezionale> items)
        {
            rdTotali = new GestionePolveriLib.BL.Reports.ReportDirezionale();
            rdTotali.Tag = "RigaTotali";

            int NP_RAL1 = 0;
            int NP_RAL2 = 0; 
            int NP_Reso  = 0;
            int NP_Smaltito = 0;
            int NP_Sublimato = 0;
            int NP_Venduto  = 0;
            int NP_Verticale  = 0;
            int NP_Ferro  = 0;
            int NP_Correzione  = 0;
            int NP_ColorwoodPiccolo = 0;
            int NP_ColorwoodGrande  = 0;
            int NP_AllineamentoInventatio = 0;
            
            double m_carico = 0;
            double m_scarico = 0;
            double m_scortaMinima = 0;
            double m_giacenza = 0;
            double m_giacenzaXValore = 0;

            double m_consAllineamentoInventario = 0;
            double m_consColorwoodGrande = 0;
            double m_consColorwoodPiccolo = 0;
            double m_consCorrezione = 0;
            double m_consFerro = 0;
            double m_consRAL1 = 0;
            double m_consRAL2 = 0;
            double m_consReso = 0;
            double m_consSmaltito = 0;
            double m_consSublimato = 0;
            double m_consVenduto = 0;
            double m_consVerticale = 0;
            
            foreach (GestionePolveriLib.BL.Reports.ReportDirezionale item in items)
            {
                rdTotali.NumeroPrelieviTot += item.NumeroPrelievi;

                NP_RAL1 += item.NP_RAL1.GetValueOrDefault(0);
                NP_RAL2 += item.NP_RAL2.GetValueOrDefault(0);
                NP_Reso += item.NP_Reso.GetValueOrDefault(0);
                NP_Smaltito += item.NP_Smaltito.GetValueOrDefault(0);
                NP_Sublimato += item.NP_Sublimato.GetValueOrDefault(0);
                NP_Venduto += item.NP_Venduto.GetValueOrDefault(0);
                NP_Verticale += item.NP_Verticale.GetValueOrDefault(0);
                NP_Ferro += item.NP_Ferro.GetValueOrDefault(0);
                NP_Correzione += item.NP_Correzione.GetValueOrDefault(0);
                NP_ColorwoodPiccolo += item.NP_ColorwoodPiccolo.GetValueOrDefault(0);
                NP_ColorwoodGrande += item.NP_ColorwoodGrande.GetValueOrDefault(0);
                NP_AllineamentoInventatio += item.NP_AllineamentoInventatio.GetValueOrDefault(0);

                m_carico += item.Carico.GetValueOrDefault(0);
                m_scarico += item.Scarico.GetValueOrDefault(0);
                m_giacenza += item.Giacenza.GetValueOrDefault(0);
                m_giacenzaXValore += item.GiacenzaXValore.GetValueOrDefault(0);

                m_consAllineamentoInventario += item.Cons_AllineamentoInventario.GetValueOrDefault(0);
                m_consColorwoodGrande += item.Cons_ColorwoodGrande.GetValueOrDefault(0);
                m_consColorwoodPiccolo += item.Cons_ColorwoodPiccolo.GetValueOrDefault(0);
                m_consCorrezione += item.Cons_Correzione.GetValueOrDefault(0);
                m_consFerro += item.Cons_Ferro.GetValueOrDefault(0);
                m_consRAL1 += item.Cons_RAL1.GetValueOrDefault(0);
                m_consRAL2 += item.Cons_RAL2.GetValueOrDefault(0);
                m_consReso += item.Cons_Reso.GetValueOrDefault(0);
                m_consSmaltito += item.Cons_Smaltito.GetValueOrDefault(0);
                m_consSublimato += item.Cons_Sublimato.GetValueOrDefault(0);
                m_consVenduto += item.Cons_Venduto.GetValueOrDefault(0);
                m_consVerticale += item.Cons_Verticale.GetValueOrDefault(0);

                m_scortaMinima += item.ScortaMinima;

                //rd.NP_RAL1 += (item.NP_RAL1 == null ? (int?)0 : (int?)item.NP_RAL1);
                //rd.NP_RAL2 += (item.NP_RAL2 == null ? (int?)0 : (int?)item.NP_RAL2);
                //rd.NP_Reso += (item.NP_Reso == null ? (int?)0 : (int?)item.NP_Reso);
                //rd.NP_Smaltito += (item.NP_Smaltito == null ? (int?)0 : (int?)item.NP_Smaltito);
                //rd.NP_Sublimato += (item.NP_Sublimato == null ? (int?)0 : (int?)item.NP_Sublimato);
                //rd.NP_Venduto += (item.NP_Venduto == null ? (int?)0 : (int?)item.NP_Venduto);
                //rd.NP_Verticale += (item.NP_Verticale == null ? (int?)0 : (int?)item.NP_Verticale);
                //rd.NP_Ferro += (item.NP_Ferro == null ? (int?)0 : (int?)item.NP_Ferro);
                //rd.NP_Correzione += (item.NP_Correzione == null ? (int?)0 : (int?)item.NP_Correzione);
                //rd.NP_ColorwoodPiccolo += (item.NP_ColorwoodPiccolo == null ? (int?)0 : (int?)item.NP_ColorwoodPiccolo);
                //rd.NP_ColorwoodGrande += (item.NP_ColorwoodGrande == null ? (int?)0 : (int?)item.NP_ColorwoodGrande);
                //rd.NP_AllineamentoInventatio += (item.NP_AllineamentoInventatio == null ? (int?)0 : (int?)item.NP_AllineamentoInventatio);
            }

            rdTotali.NP_RAL1 = NP_RAL1;
            rdTotali.NP_RAL2 = NP_RAL2;
            rdTotali.NP_Reso = NP_Reso;
            rdTotali.NP_Smaltito = NP_Smaltito;
            rdTotali.NP_Sublimato = NP_Sublimato;
            rdTotali.NP_Venduto = NP_Venduto;
            rdTotali.NP_Verticale = NP_Verticale;
            rdTotali.NP_Ferro = NP_Ferro;
            rdTotali.NP_Correzione = NP_Correzione;
            rdTotali.NP_ColorwoodPiccolo = NP_ColorwoodPiccolo;
            rdTotali.NP_ColorwoodGrande = NP_ColorwoodGrande;
            rdTotali.NP_AllineamentoInventatio = NP_AllineamentoInventatio;

            rdTotali.Cons_Verticale = m_consVerticale;
            rdTotali.Cons_Venduto = m_consVenduto;
            rdTotali.Cons_Sublimato = m_consSublimato;
            rdTotali.Cons_Smaltito = m_consSmaltito;
            rdTotali.Cons_Reso = m_consReso;
            rdTotali.Cons_RAL2 = m_consRAL2;
            rdTotali.Cons_RAL1 = m_consRAL1;
            rdTotali.Cons_Ferro = m_consFerro;
            rdTotali.Cons_Correzione = m_consCorrezione;
            rdTotali.Cons_ColorwoodPiccolo = m_consColorwoodPiccolo;
            rdTotali.Cons_ColorwoodGrande = m_consColorwoodGrande;
            rdTotali.Cons_AllineamentoInventario = m_consAllineamentoInventario;

            rdTotali.Carico = m_carico;
            rdTotali.Scarico = m_scarico;
            rdTotali.ScortaMinima = m_scortaMinima;
            rdTotali.Giacenza = m_giacenza;

            rdTotali.UseCalculatedField = false;
            rdTotali.GiacenzaXValore = m_giacenzaXValore;

            items.Insert(0, rdTotali);

            PrintTotali(rdTotali);

            return items;
        }

        private void PrintTotali(ReportDirezionale rdTotali)
        {
            this.label1.Text = string.Format("Giacenza: {0:N}, Valore Giacenza: {1:N}, Tot. Carico: {2:N} Tot. Scarico: {3:N}", rdTotali.Giacenza, rdTotali.GiacenzaXValore, rdTotali.Carico, rdTotali.Scarico);

            this.label2.Text = string.Format("Ral1: {0:N} / {1:N}, Ral2: {2:N} / {3:N}, Vert.: {4:N} / {5:N}, Sublim.: {6:N} / {7:N}, Ferro: {8:N} / {9:N} Colorwood Grande: {10:N} / {11:N}, Colorwood Piccolo: {12:N} / {13:N}", 
                                                  rdTotali.Cons_RAL1, rdTotali.NP_RAL1,
                                                  rdTotali.Cons_RAL2, rdTotali.NP_RAL2,
                                                  rdTotali.Cons_Verticale, rdTotali.NP_Verticale,
                                                  rdTotali.Cons_Sublimato, rdTotali.NP_Sublimato,
                                                  rdTotali.Cons_Ferro, rdTotali.NP_Ferro,
                                                  rdTotali.Cons_ColorwoodGrande, rdTotali.NP_ColorwoodGrande,
                                                  rdTotali.Cons_ColorwoodPiccolo, rdTotali.NP_ColorwoodPiccolo);

            this.label3.Text = string.Format("Reso: {0:N} / {1:N}, Smaltito: {2:N} / {3:N}, Venduto: {4:N} / {5:N}, Inventario: {6:N} / {7:N}, Allineamento inv.: {8:N} / {9:N}",
                                             rdTotali.Cons_Reso, rdTotali.NP_Reso,
                                             rdTotali.Cons_Smaltito, rdTotali.NP_Smaltito,
                                             rdTotali.Cons_Venduto, rdTotali.NP_Venduto,
                                             rdTotali.Cons_Correzione, rdTotali.NP_Correzione,
                                             rdTotali.Cons_AllineamentoInventario, rdTotali.NP_AllineamentoInventatio);

            this.label1.Visible = this.label2.Visible = this.label3.Visible = true;
        }

        private void NascondiColonne()
        {
            //this.olvColumnPrezzo.IsVisible = false;
            //this.olvColumnValore.IsVisible = false;

            //OLVColumn c = this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_RAL1"); 

            OperatorePrincipal m_OperatorePrincipale = (OperatorePrincipal)System.Threading.Thread.CurrentPrincipal;
            if ((!m_OperatorePrincipale.IsInRole("Amministrazione")) && (!m_OperatorePrincipale.IsInRole("Direzione")))
            {
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_RAL1"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_RAL1"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_RAL2"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_RAL2"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_Verticale"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_Verticale"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_Ferro"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_Ferro"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_ColorwoodGrande"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_ColorwoodGrande"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_ColorwoodPiccolo"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_ColorwoodPiccolo"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_Reso"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_Reso"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_Smaltito"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_Smaltito"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_Sublimato"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_Sublimato"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_Venduto"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_Venduto"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_Correzione"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_Correzione"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "Cons_AllineamentoInventario"));
                this.objectListView1.AllColumns.Remove(this.objectListView1.AllColumns.First(x => x.AspectName == "NP_AllineamentoInventatio"));

                this.objectListView1.RebuildColumns();

                columnsToHide = new string[] { 
                                        "Cons_RAL1", "NP_RAL1", 
                                        "Cons_RAL2", "NP_RAL2", 
                                        "Cons_Verticale", "NP_Verticale", 
                                        "Cons_Ferro", "NP_Ferro", 
                                        "Cons_ColorwoodGrande", "NP_ColorwoodGrande", 
                                        "Cons_ColorwoodPiccolo", "NP_ColorwoodPiccolo",
                                        "Cons_Reso", "NP_Reso", 
                                        "Cons_Smaltito", "NP_Smaltito", 
                                        "Cons_Sublimato", "NP_Sublimato", 
                                        "Cons_Venduto", "NP_Venduto",
                                        "Cons_Correzione", "NP_Correzione",
                                        "Cons_AllineamentoInventario", "NP_AllineamentoInventatio"
                            };
            }
        }

        public override void OnLoadConfigurazioneMaschera()
        {
            base.OnLoadConfigurazioneMaschera();
        }

        public override void OnRipristinaConfigurazioneDefaultMaschera()
        {
            base.OnRipristinaConfigurazioneDefaultMaschera();
        }

        public override void OnSalvaConfigurazioneMaschera()
        {
            base.OnSalvaConfigurazioneMaschera();

            byte[] data = this.objectListView1.SaveState();

            System.Security.Principal.IPrincipal m_operatore = System.Threading.Thread.CurrentPrincipal;
            string[] roles = System.Web.Security.Roles.GetRolesForUser(m_operatore.Identity.Name);

            SavingConfigurationBag scb = new SavingConfigurationBag();
            scb.FormName = this.Name;
            scb.RoleName = roles[0];
            //scb.UserName = m_operatore.Identity.Name;

            ColumnsConfigurationManager ccm = new ColumnsConfigurationManager();
            ccm.Configuration = scb;
            ccm.State = data;
            ccm.SaveState();
        }
    }

    public class ReportDirezionaleBase : AnagraficaManagerFilteredObjectListView<ReportDirezionale, ReportDirezionaleFilter, ReportDirezionaleFilterBag> { }
}
